package sync.ssc;

import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sync.bean.PKBean;
import sync.bean.PKDataBean;
import sync.bean.PKUpdateBean;
import sync.bean.PKzsBean;
import sync.bean.SSCBean;
import sync.db.ConnectionPool;
import sync.db.DBHelper;
import sync.utils.Utils;

public class SyncPK {

	private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

	// 获取PK开奖数据 有开奖时间
	public boolean getDataFromGuanWang()throws Exception{
		URL url = new URL("http://www.bwlc.net/bulletin/trax.html");
		Document doc = Jsoup.parse(url, 5000);
		Elements elements = doc.getElementsByTag("table");
		Elements trs = elements.get(1).getElementsByTag("tr");
		Element tr = trs.get(1);
		Elements tds = tr.getElementsByTag("td");
		String qihao = tds.get(0).html();
		String[] numbers = tds.get(1).html().split(",");
		String opentime = tds.get(2).html();
		String one = numbers[0];
		String two = numbers[1];
		String three = numbers[2];
		String four = numbers[3];
		String five = numbers[4];
		String six = numbers[5];
		String seven = numbers[6];
		String eight = numbers[7];
		String nine = numbers[8];
		String ten = numbers[9];
		PKBean bean = new PKBean();
		bean.setQihao(qihao);
		bean.setOne(getInt(one));
		bean.setTwo(getInt(two));
		bean.setThree(getInt(three));
		bean.setFour(getInt(four));
		bean.setFive(getInt(five));
		bean.setSix(getInt(six));
		bean.setSeven(getInt(seven));
		bean.setEight(getInt(eight));
		bean.setNine(getInt(nine));
		bean.setTen(getInt(ten));
		bean.setOpentime(opentime);

		PKzsBean zBean = new PKzsBean();
		zBean.setQihao(bean.getQihao());
		zBean.setOne(bean.getOne()+"");
		zBean.setTwo(bean.getTwo()+"");
		int sum = (bean.getOne()+bean.getTwo());
		zBean.setSumot(sum+"");
		String area = "-1";
		if(sum >=3 && sum < 11){
			area = "0";
		}else{
			area = "1";
		}
		zBean.setArea(area);
		String level = "-1";
		switch(sum){
		case 3:
		case 4:
		case 18:
		case 19:
			level = "0";
			break;
		case 5:
		case 6:
		case 16:
		case 17:
			level = "1";
			break;
		case 7:
		case 8:
		case 14:
		case 15:
			level = "2";
			break;
		case 9:
		case 10:
		case 12:
		case 13:
			level = "3";
			break;
		}
		zBean.setSumlevel(level);

		insertPkzs(zBean);

		updatePkRecord(bean.getQihao(),bean);
		return true;
	}

	// 更新当前期数据
	private void updatePkRecord(String qihao,PKBean bean){
		PKUpdateBean pBean = null;
		PKUpdateBean iBean = null;
		String tableName = "";
		int k = -1;
		for(int i = 0; i < 10; i++){
			iBean = new PKUpdateBean();
			switch(i){
			case 0:
				tableName = "pk_one";
				k = bean.getOne();
				break;
			case 1:
				tableName = "pk_two";
				k = bean.getTwo();
				break;
			case 2:
				tableName = "pk_three";
				k = bean.getThree();
				break;
			case 3:
				k = bean.getFour();
				tableName = "pk_four";
				break;
			case 4:
				k = bean.getFive();
				tableName = "pk_five";
				break;
			case 5:
				k = bean.getSix();
				tableName = "pk_six";
				break;
			case 6:
				k = bean.getSeven();
				tableName = "pk_seven";
				break;
			case 7:
				k = bean.getEight();
				tableName = "pk_eight";
				break;
			case 8:
				k = bean.getNine();
				tableName = "pk_nine";
				break;
			case 9:
				k = bean.getTen();
				tableName = "pk_ten";
				break;
			}
			pBean = DBHelper.getPKUpdateByQihao(qihao, tableName);
			if(pBean == null) return;
			if(k == (Integer.parseInt(pBean.getOne()))){
				iBean.setOnewr("1");
			}else{
				iBean.setOnewr("0");
			}
			if(k == (Integer.parseInt(pBean.getTwo()))){
				iBean.setTwowr("1");
			}else{
				iBean.setTwowr("0");
			}
			if(k == (Integer.parseInt(pBean.getThree()))){
				iBean.setThreewr("1");
			}else{
				iBean.setThreewr("0");
			}
			if(k == (Integer.parseInt(pBean.getFour()))){
				iBean.setFourwr("1");
			}else{
				iBean.setFourwr("0");
			}
			if(k == (Integer.parseInt(pBean.getFive()))){
				iBean.setFivewr("1");
			}else{
				iBean.setFivewr("0");
			}
			if(k == (Integer.parseInt(pBean.getSix()))){
				iBean.setSixwr("1");
			}else{
				iBean.setSixwr("0");
			}
			if(k == (Integer.parseInt(pBean.getSeven()))){
				iBean.setSevenwr("1");
			}else{
				iBean.setSevenwr("0");
			}
			if(k == (Integer.parseInt(pBean.getEight()))){
				iBean.setEightwr("1");
			}else{
				iBean.setEightwr("0");
			}
			if(k == (Integer.parseInt(pBean.getNine()))){
				iBean.setNinewr("1");
			}else{
				iBean.setNinewr("0");
			}
			if(k == (Integer.parseInt(pBean.getTen()))){
				iBean.setTenwr("1");
			}else{
				iBean.setTenwr("0");
			}
			iBean.setResult(k+"");
			iBean.setQihao(qihao);
			DBHelper.updateLastPKRecord(tableName,iBean);
		}
	}

	// 插入走势
	private void insertPkzs(PKzsBean bean){
		if(!isExist(bean.getQihao())){
			DBHelper.insertPkzs(bean);
			insertPKOT(bean.getQihao());
		}
	}
	// 插入随机数据
	private void insertPKOT(String qihao){
		// 更新一条新的随机记录
		Integer old = Integer.parseInt(qihao);
		String newq = (old+1)+"";
		PKDataBean bean = null;
		for(int i = 0; i < 10; i++){
			bean = new PKDataBean();
			bean.setQihao(newq);
			bean.setOne(Utils.getRandomOne()+"");
			bean.setTwo(Utils.getRandomOne()+"");
			bean.setThree(Utils.getRandomOne()+"");
			bean.setFour(Utils.getRandomOne()+"");
			bean.setFive(Utils.getRandomOne()+"");
			bean.setSix(Utils.getRandomOne()+"");
			bean.setSeven(Utils.getRandomOne()+"");
			bean.setEight(Utils.getRandomOne()+"");
			bean.setNine(Utils.getRandomOne()+"");
			bean.setTen(Utils.getRandomOne()+"");
			String tableName = "";
			switch(i){
			case 0:
				tableName = "pk_one";
				break;
			case 1:
				tableName = "pk_two";
				break;
			case 2:
				tableName = "pk_three";
				break;
			case 3:
				tableName = "pk_four";
				break;
			case 4:
				tableName = "pk_five";
				break;
			case 5:
				tableName = "pk_six";
				break;
			case 6:
				tableName = "pk_seven";
				break;
			case 7:
				tableName = "pk_eight";
				break;
			case 8:
				tableName = "pk_nine";
				break;
			case 9:
				tableName = "pk_ten";
				break;
			}
			bean.setTable_name(tableName);
			DBHelper.insertPkOneToTen(bean);
		}
	}

	private boolean isExist(String qihao){
		String newQh = DBHelper.getPKLastRecordQihao();
		if(newQh.equals(qihao)){
			return true;
		}
		return false;
	}

	private int getInt(String num){
		if(num.startsWith("0")){
			num = num.substring(1, 2);
		}
		return Integer.parseInt(num);
	}

}
